<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        title: {
          text: "教学质量评价",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },

        legend: {
          data: ["good", "bad"],
          textStyle: {
            color: "#111",
          },
          itemStyle: {
            color: ["#5a7fb7", "#b25752"],
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        yAxis: {
          type: "value",
          show: true, // 不显示坐标轴线、坐标轴刻度线和坐标轴上的文字
          axisTick: {
            show: true, // 不显示坐标轴刻度线
          },
          axisLine: {
            show: true, // 不显示坐标轴线
          },
          axisLabel: {
            show: true, // 不显示坐标轴上的文字
          },
          splitLine: {
            show: true, // 不显示网格线

            lineStyle: {
              color: ["#999"],
              width: 2,
              type: "solid",
            },
          },
        },
        xAxis: {
          type: "category",

          data: ["教师", "讲师", "副教授", "教授"],
        },
        series: [
          {
            name: "bad",
            type: "bar",
            data: [75, 30, 20, 10],
            itemStyle: {
              normal: {
                borderColor: function (params) {
                  let bcolorList = [
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                  ];
                  return bcolorList[params.dataIndex];
                },
                color: function (params) {
                  let colorList = [
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                    "#5a7fb7",
                  ];
                  return colorList[params.dataIndex];
                },
              },
            },
          },
          {
            name: "good",
            type: "bar",
            data: [66, 77, 88, 99],
            itemStyle: {
              normal: {
                color: function (params) {
                  let colorList = [
                    "#b25752",
                    "#b25752",
                    "#b25752",
                    "#b25752",
                    "#b25752",
                    "#b25752",
                    "#b25752",
                  ];
                  return colorList[params.dataIndex];
                },
              },
            },
          },
        ],
      });
    },
  },
};
</script>
